<?php
namespace app\massage\controller;
use app\AdminRest;
use app\ApiRest;
use app\massage\model\City;
use app\massage\model\Coach;
use app\massage\model\CoachLevel;
use app\massage\model\Commission;
use app\massage\model\Config;
use app\massage\model\DistributionList;
use app\massage\model\Order;
use app\massage\model\User;
use app\massage\model\Wallet;
use longbingcore\wxcore\YsCloudApi;
use think\App;



class IndexReseller extends ApiRest
{


    protected $model;

    protected $user_model;

    protected $cash_model;

    protected $wallet_model;

    protected $coach_model;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->model        = new DistributionList();

        $this->user_model   = new User();

        $this->cash_model   = new Commission();

        $this->wallet_model = new Wallet();

        $this->coach_model  = new Coach();

    }


    /**
     * @author chenniang
     * @DataTime: 2023-03-23 13:49
     * @功能说明:合伙人中心
     */
    public function partnerIndex(){

        $order_model = new Order();
        //超时自动取消订单
        $order_model->coachBalanceArr($this->_uniacid);

        $data = $this->user_model->dataInfo(['id'=>$this->_user['id']],'nickName,avatarUrl,new_cash,cash');

        $data['order_cash'] = $this->model->partnerOrderPrice($this->_user['id']);
        //已提现金额
        $data['wallet_cash'] = $this->wallet_model->where(['user_id'=>$this->_user['id'],'type'=>4])->where('status','<>',3)->sum('total_price');
        //未入账
        $data['not_recorded']= $this->cash_model->where(['top_id'=>$this->_user['id'],'status'=>1])->where('type','in',[1,9])->sum('cash');
        //累计订单量
        $data['total_order_count'] = $this->cash_model->where(['top_id'=>$this->_user['id'],'status'=>2])->where('type','in',[1,9])->group('order_id')->count();
        //今日订单数量
        $data['today_order_count'] = $this->cash_model->where(['top_id'=>$this->_user['id'],'status'=>2])->where('type','in',[1,9])->whereTime('create_time','today')->group('order_id')->count();
        //累计邀请技师
        $data['total_coach_count'] = $this->coach_model->where(['partner_id'=>$this->_user['id'],'status'=>2])->count();
        //今日邀请技师
        $data['today_coach_count'] = $this->coach_model->where(['partner_id'=>$this->_user['id'],'status'=>2])->whereTime('partner_time','today')->count();
        //累计邀请用户
        $data['total_user_count'] = $this->user_model->where(['pid'=>$this->_user['id']])->count();
        //今日邀请用户
        $data['today_user_count'] = $this->user_model->where(['pid'=>$this->_user['id']])->whereTime('create_time','today')->count();

        $data['order_cash'] = round($data['order_cash'],2);

        $data['wallet_cash']= round($data['wallet_cash'],2);

        $data['not_recorded']= round($data['not_recorded'],2);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2023-03-23 14:30
     * @功能说明:合伙人邀请的技师
     */
    public function partnerCoachList(){

        $dis = [

            'status'     => 2,

            'partner_id' => $this->_user['id']
        ];

        $data = $this->coach_model->where($dis)->field('admin_id,id,coach_name,work_img,city_id')->order('partner_time desc,id desc')->paginate(10)->toArray();

        if(!empty($data['data'])){

            $config_model = new Config();

            $level_model  = new CoachLevel();

            $city_model   = new City();

            $admin_model = new \app\massage\model\Admin();

            $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);

            $level_cycle = $config['level_cycle'];

            $is_current  = $config['is_current'];

            foreach ($data['data'] as &$v){

                $v['order_count'] = $level_model->getMinCount($v['id'],$level_cycle,0,$is_current);

                $v['city'] = $city_model->where(['id'=>$v['city_id']])->value('city');

                $admin = $admin_model->dataInfo(['id'=>$v['admin_id'],'status'=>1]);
                //代理商
                $v['admin_name'] = !empty($admin)?$admin['agent_name']:'平台';
            }

        }
        return $this->success($data);
    }



    /**
     * @author chenniang
     * @DataTime: 2022-08-30 14:18
     * @功能说明 合伙人邀请技师码
     */
    public function resellerInvCoachQr(){

        $input = $this->_param;

        $admin_id = !empty($input['admin_id'])?$input['admin_id']:0;

        $key = 'resellerInvCoach_qr'.$this->_user['id'].'-'.$this->is_app.'-'.$admin_id;

        $qr  = getCache($key,$this->_uniacid);

        if(empty($qr)){
            //小程序
            if($this->is_app==0){

                $input['page'] = 'technician/pages/apply';

                $input['partner_id'] = $this->_user['id'];
                //获取二维码
                $qr = $this->user_model->orderQr($input,$this->_uniacid);

            }else{

                $page = 'https://'.$_SERVER['HTTP_HOST'].'/h5/#/technician/pages/apply?partner_id='.$this->_user['id'].'&admin_id='.$input['admin_id'];

                $qr = base64ToPng(getCode($this->_uniacid,$page));

            }

            setCache($key,$qr,86400,$this->_uniacid);
        }

        $qr = !empty($qr)?$qr:'https://'.$_SERVER['HTTP_HOST'].'/favicon.ico';

        return $this->success($qr);

    }


    /**
     * @author chenniang
     * @DataTime: 2023-05-09 10:43
     * @功能说明:
     */
    public function adminList(){

        $admin_model = new \app\massage\model\Admin();

        $dis = [

            'status'       => 1,

            'is_admin'     => 0,

            'partner_auth' => 1
        ];

        $input = $this->_param;

        $where = [];

        if(!empty($input['nickName'])){

            $where[] =['agent_name','like','%'.$input['nickName'].'%'];
        }

        $data = $admin_model->where($dis)->where($where)->field('agent_name,id,city_type,user_id')->paginate(10)->toArray();

        $user_model = new User();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['avatarUrl'] = $user_model->where(['id'=>$v['user_id']])->value('avatarUrl');
            }
        }

        return $this->success($data);
    }
















}
